package org.example.ttms.entityClass;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.collections.ObservableList;
import org.example.ttms.controller.ManagerController;
import org.example.ttms.entityClass.train.Station;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Manager {
    private String id;
    private String name;
    private String account;
    private String password;

    public Manager() {
    }

    public Manager(String id, String account, String password) {
        this.id = id;
        this.account = account;
        this.password = password;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    //根据用户account获取整个用户
    public Manager obtainManager(String account){
        try {
            String file = "src/main/java/org/example/ttms/file/manager.txt";
            Scanner scan = new Scanner(new FileReader(file));
            while(scan.hasNextLine()){
                String str = scan.nextLine();
                ObjectMapper objectMapper = new ObjectMapper();
                Manager manager = objectMapper.readValue(str, Manager.class);
                if(manager.account.equals(account)){
                    return manager;
                }
            }
        } catch (FileNotFoundException | JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    //获取所有管理员
    public HashMap<String, Manager> obtainAllManager(){
        HashMap<String, Manager> hashMap = new HashMap<>();
        try {
            String file = "src/main/java/org/example/ttms/file/manager.txt";
            Scanner scan = new Scanner(new FileReader(file));
            while(scan.hasNextLine()){
                String str = scan.nextLine();
                ObjectMapper objectMapper = new ObjectMapper();
                Manager manager = objectMapper.readValue(str, Manager.class);
                hashMap.put(manager.id, manager);
            }
        } catch (FileNotFoundException | JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return hashMap;
    }
    //注册时给一个管理员id
    public String obtainId(){
        HashMap<String, Manager> hashMap = obtainAllManager();
        String enrollId = "Manager0";
        for(Map.Entry<String, Manager> map : hashMap.entrySet()){
            enrollId = map.getKey();
        }
        return "Manager" + (Integer.parseInt(enrollId.split("Manager")[1]) + 1);
    }
    //添加管理员
    public void addManager(Manager manager){
        ObjectMapper objectMapper = new ObjectMapper();
        String file = "src/main/java/org/example/ttms/file/manager.txt";
        try {
            String jsonString = objectMapper.writeValueAsString(manager);
            System.out.println("addManager:"+jsonString);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter
                    (new FileOutputStream(file, true)));
            writer.write(jsonString+"\n");
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //重新储存管理员信息---覆盖
    public void storageManager(ObservableList<Manager> observableList){
        ObjectMapper objectMapper = new ObjectMapper();
        String file = "src/main/java/org/example/ttms/file/manager.txt";
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter
                    (new FileOutputStream(file)));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        for(Manager manager : observableList) {
            try {
                String jsonString = objectMapper.writeValueAsString(manager);
                writer.write(jsonString + "\n");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
